package com.haixiaoke.saas.ad.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.ad.domain.ApartmentAd;
import com.haixiaoke.saas.ad.mapper.ApartmentAdMapper;
import com.haixiaoke.saas.ad.service.IApartmentAdService;
import com.haixiaoke.saas.common.constant.ApartmentAdConstants;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.system.framework.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公寓广告Service业务层处理
 *
 * @author qixi
 * @date 2024-04-26
 */
@Service
public class ApartmentAdServiceImpl implements IApartmentAdService {
    @Autowired
    private ApartmentAdMapper apartmentAdMapper;

    /**
     * 查询公寓广告
     *
     * @param bannerId 公寓广告主键
     * @return 公寓广告
     */
    @Override
    public ApartmentAd selectApartmentAdByBannerId(Long bannerId) {
        return apartmentAdMapper.selectApartmentAdByBannerId(bannerId);
    }

    /**
     * 查询公寓广告列表
     *
     * @param apartmentAd 公寓广告
     * @return 公寓广告
     */
    @Override
    public List<ApartmentAd> selectApartmentAdList(ApartmentAd apartmentAd) {
        return apartmentAdMapper.selectApartmentAdList(apartmentAd);
    }

    /**
     * 新增公寓广告
     *
     * @param apartmentAd 公寓广告
     * @return 结果
     */
    @Override
    public int insertApartmentAd(ApartmentAd apartmentAd) {
        List<ApartmentAd> apartmentAdList =  apartmentAdMapper.selectApartmentAdByLocation(apartmentAd.getLocation());
        if (apartmentAdList.size() > 6) {
            throw new ServiceException("轮播图最多只能有6张，请先删除无用的轮播图");
        }

        if (ApartmentAdConstants.AD_TYPE_CON.equals(apartmentAd.getAdType())) {
            if (ApartmentAdConstants.LOCATION_TOP.equals(apartmentAd.getLocation())) {
                if (ApartmentAdConstants.AREA_TYPE_DEFAULT.equals(apartmentAd.getAreaType())) {
                    ApartmentAd sqlApartmentAd =  apartmentAdMapper.selectApartmentAdByAreaType(apartmentAd.getAreaType());
                    if (ObjectUtil.isNotEmpty(sqlApartmentAd)) {
                        throw new ServiceException("背景图已存在，请前往修改背景图");
                    }
                } else {
                    ApartmentAd sqlApartmentAd =  apartmentAdMapper.selectApartmentAdByCityCode(apartmentAd.getCityCode());
                    if (ObjectUtil.isNotEmpty(sqlApartmentAd)) {
                        throw new ServiceException("背景图已存在，请前往修改背景图");
                    }
                }
            }
        }


        apartmentAd.setCreateTime(DateUtils.getNowDate());
        apartmentAd.setCreateBy(SecurityUtils.getUsername());
        return apartmentAdMapper.insertApartmentAd(apartmentAd);
    }

    /**
     * 修改公寓广告
     *
     * @param apartmentAd 公寓广告
     * @return 结果
     */
    @Override
    public int updateApartmentAd(ApartmentAd apartmentAd) {
        List<ApartmentAd> apartmentAdList =  apartmentAdMapper.selectApartmentAdByLocation(apartmentAd.getLocation());
        if (apartmentAdList.size() > 6) {
            throw new ServiceException("轮播图最多只能有6张，请先删除无用的轮播图");
        }

        if (ApartmentAdConstants.AD_TYPE_CON.equals(apartmentAd.getAdType())) {
            if (ApartmentAdConstants.LOCATION_TOP.equals(apartmentAd.getLocation())) {
                if (ApartmentAdConstants.AREA_TYPE_DEFAULT.equals(apartmentAd.getAreaType())) {
                    ApartmentAd sqlApartmentAd =  apartmentAdMapper.selectApartmentAdByAreaType(apartmentAd.getAreaType());
                    if (ObjectUtil.isNotEmpty(sqlApartmentAd) && !sqlApartmentAd.getBannerId().equals(apartmentAd.getBannerId())) {
                        throw new ServiceException("背景图已存在，请前往修改背景图");
                    }
                } else {
                    ApartmentAd sqlApartmentAd =  apartmentAdMapper.selectApartmentAdByCityCode(apartmentAd.getCityCode());
                    if (ObjectUtil.isNotEmpty(sqlApartmentAd) && !sqlApartmentAd.getBannerId().equals(apartmentAd.getBannerId())) {
                        throw new ServiceException("背景图已存在，请前往修改背景图");
                    }
                }
            }
        }

        apartmentAd.setUpdateTime(DateUtils.getNowDate());
        apartmentAd.setUpdateBy(SecurityUtils.getUsername());
        return apartmentAdMapper.updateApartmentAd(apartmentAd);
    }

    /**
     * 批量删除公寓广告
     *
     * @param bannerIds 需要删除的公寓广告主键
     * @return 结果
     */
    @Override
    public int deleteApartmentAdByBannerIds(Long[] bannerIds) {
        return apartmentAdMapper.deleteApartmentAdByBannerIds(bannerIds);
    }

    /**
     * 删除公寓广告信息
     *
     * @param bannerId 公寓广告主键
     * @return 结果
     */
    @Override
    public int deleteApartmentAdByBannerId(Long bannerId) {
        return apartmentAdMapper.deleteApartmentAdByBannerId(bannerId);
    }

    @Override
    public List<ApartmentAd> selectFrontApartmentAdList(ApartmentAd apartmentAd) {
        List<ApartmentAd> apartmentAdList = null;
        if (ApartmentAdConstants.LOCATION_TOP.equals(apartmentAd.getLocation())) {
            apartmentAdList = apartmentAdMapper.selectApartmentAdByCityCodeAndLimit(apartmentAd.getCityCode());
        } else {
            apartmentAdList = apartmentAdMapper.selectApartmentAdByCityCodeAndLocation(apartmentAd.getCityCode(), apartmentAd.getLocation());
        }
        return apartmentAdList;
    }
}
